﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WorkNodeFramework
{
    public class Table<T>: Table
        where T:class
    {
        public Table(DataProvider provider, string Name) :base(provider,Name){ 

        }
    }

    internal class StringTable : Table {
        public StringTable(string tableName) 
            :base(DataService.Provider,tableName)
        { 
            
        }
    }

    public class Table:ITable
    {
        DataProvider provider;
        List<Column> cols = new List<Column>();
        string name;

        public Table(DataProvider provider,string TableName) {
            this.name = TableName;
            this.provider = provider;
        }

        public Column this[string ColumnName] {
            get {
                return Column(ColumnName);
            }
        }

        public Column Column(string ColumnName) {
            Column col = cols.Find(delegate(Column match) {
                return string.Compare(match.Name, ColumnName) == 0;
            });
            if (col == null)
            {
                col = new Column(this, ColumnName);
                cols.Add(col);
            }

            return col;
        }

        public Table Clone(string tableName)
        {
            Table table = new Table(provider, tableName);
            foreach (Column col in Columns)
            {
                Column ncol = new Column(table, col.Name);
                if (!string.IsNullOrEmpty(col.ColumnAlias))
                    ncol.Alias(col.ColumnAlias);
                ncol.Size = col.Size;
                ncol.SqlDbType = col.SqlDbType;
                ncol.IsNullable = col.IsNullable;
                ncol.IsAutocrement = col.IsAutocrement;
                ncol.IsPrimaryKey = col.IsPrimaryKey;

                table.Columns.Add(ncol);
            }
            return table;
        }

        #region ITable Members

        public List<Column> Columns
        {
            get { return cols; }
        }

        public string Name {
            get {
                return this.name;
            }
        }

        public virtual string FullName {
            get {
                return provider.QualifyTableName(this);
            }
        }

        AllColumnQueryExpression all = null;
        public AllColumnQueryExpression All {
            get {
                if (all == null)
                    all = new AllColumnQueryExpression(this);
                return all;
            }
        }
        public DataProvider Provider {
            get {
                return provider;
            }
        }
        #endregion
    }

    public interface ITable {
        Column this[string columnName] { get; }
        List<Column> Columns { get; }
        string Name { get; }
        string FullName { get; }
        AllColumnQueryExpression All { get; }
        DataProvider Provider { get; }
    }
}
